﻿#include "stdafx.h"
#include <llvm/IR/IRBuilder.h>
#include "BlockGen.h"

using namespace llvm;

Value * BlockGen::generateCode(Module *m, Function *func, IRBuilder<>&builder)
{
	block = BasicBlock::Create(builder.getContext(), name, func);
	if (br) builder.CreateBr(block);
	builder.SetInsertPoint(block);

	Value* expr = nullptr;
	for (auto i : codes) {
		expr=i->generateCode(m, func, builder);
	}

	if (!next) next = BasicBlock::Create(builder.getContext(), name + "_end", func);
	builder.CreateBr(next);
	builder.SetInsertPoint(next);
	return expr;
}
